home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / dev / c / AMesaRTL.lha / Mesa-2.6 / amiga / src-glut / glutMainLoop.c < prev    next >
C/C++ Source or Header  |  1998-09-19  |  9KB  |  354 lines

  1. /*
  2.  * Amiga GLUT graphics library toolkit
  3.  * Version:  2.0
  4.  * Copyright (C) 1998 Jarno van der Linden
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Library General Public
  17.  * License along with this library; if not, write to the Free
  18.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  */
  20.  
  21.  
  22. /*
  23.  * glutMainLoop.c
  24.  *
  25.  * Version 1.0  27 Jun 1998
  26.  * by Jarno van der Linden
  27.  * jarno@kcbbs.gen.nz
  28.  *
  29.  * Version 2.0  19 Sep 1998
  30.  * by Jarno van der Linden
  31.  * jarno@kcbbs.gen.nz
  32.  *
  33.  * - Changed to runtime library format
  34.  * - Added ^C checking
  35.  * - ChangeWindowBox() only when window can
  36.  * - Fixed: Mouse positions were off, added border correction
  37.  *
  38.  */
  39.  
  40.  
  41. #include <dos/dos.h>
  42.  
  43. #include <proto/intuition.h>
  44. #include <proto/gadtools.h>
  45. #include <proto/exec.h>
  46.  
  47. #include "glutstuff.h"
  48.  
  49.  
  50. extern void RedoMenu(int button, struct GlutMenu *glutmenu);
  51.  
  52.  
  53. int ConvRaw(UWORD code,UWORD qual)
  54. {
  55.     switch(code)
  56.     {
  57.         case CURSORLEFT:
  58.             return GLUT_KEY_LEFT;
  59.         case CURSORUP:
  60.             return GLUT_KEY_UP;
  61.         case CURSORRIGHT:
  62.             return GLUT_KEY_RIGHT;
  63.         case CURSORDOWN:
  64.             return GLUT_KEY_DOWN;
  65.         case 0x50:
  66.             return GLUT_KEY_F1;
  67.         case 0x51:
  68.             return GLUT_KEY_F2;
  69.         case 0x52:
  70.             return GLUT_KEY_F3;
  71.         case 0x53:
  72.             return GLUT_KEY_F4;
  73.         case 0x54:
  74.             return GLUT_KEY_F5;
  75.         case 0x55:
  76.             return GLUT_KEY_F6;
  77.         case 0x56:
  78.             return GLUT_KEY_F7;
  79.         case 0x57:
  80.             return GLUT_KEY_F8;
  81.         case 0x58:
  82.             return GLUT_KEY_F9;
  83.         case 0x59:
  84.             return GLUT_KEY_F10;
  85.     }
  86.  
  87.     return 0;
  88. }
  89.  
  90.  
  91. int ConvToButton(UWORD code,UWORD qual)
  92. {
  93.     switch(code)
  94.     {
  95.         case SELECTUP:
  96.         case SELECTDOWN:
  97.             if(qual & IEQUALIFIER_RCOMMAND)
  98.                 return GLUT_MIDDLE_BUTTON;
  99.             if(qual & IEQUALIFIER_RALT)
  100.                 return GLUT_RIGHT_BUTTON;
  101.             return GLUT_LEFT_BUTTON;
  102.         case MIDDLEUP:
  103.         case MIDDLEDOWN:
  104.             return GLUT_MIDDLE_BUTTON;
  105.         case MENUUP:
  106.         case MENUDOWN:
  107.             return GLUT_RIGHT_BUTTON;
  108.     }
  109.  
  110.     return 0;
  111. }
  112.  
  113.  
  114. int ConvToButtonState(UWORD code)
  115. {
  116.     switch(code)
  117.     {
  118.         case SELECTUP:
  119.         case MIDDLEUP:
  120.         case MENUUP:
  121.             return GLUT_UP;
  122.         case SELECTDOWN:
  123.         case MIDDLEDOWN:
  124.         case MENUDOWN:
  125.             return GLUT_DOWN;
  126.     }
  127.  
  128.     return 0;
  129. }
  130.  
  131.  
  132. __asm __saveds void glutMainLoop( void )
  133. {
  134.     struct IntuiMessage *msg, cmsg;
  135.     struct GlutWindow *gw;
  136.     struct GlutMenu *gm;
  137.     struct GlutMenuEntry *gme;
  138.     struct MenuItem *item;
  139.     UWORD menuNumber;
  140.     BOOL idleing, wanttoquit;
  141.     ULONG signals;
  142.  
  143.     wanttoquit = FALSE;
  144.     while(1)
  145.     {
  146.         // Apply changes
  147.         //
  148.         for(gw = glutstuff.wins; gw; gw = gw->next)
  149.         {
  150.             stuffMakeCurrent(gw);
  151.  
  152.             if(gw->needposition)
  153.             {
  154.                 gw->needposition = FALSE;
  155.                 gw->needpositiongui = FALSE;
  156.                 // Let the change be made through the gui
  157.                 if(gw->window->Flags & WFLG_DRAGBAR)
  158.                     ChangeWindowBox(gw->window,gw->winx,gw->winy,gw->winwidth,gw->winheight);
  159.             }
  160.  
  161.             if(gw->needpositiongui)
  162.             {
  163.                 gw->needpositiongui = FALSE;
  164.             }
  165.  
  166.             if(gw->needreshape)
  167.             {
  168.                 gw->needreshape = FALSE;
  169.                 gw->needreshapegui = FALSE;        /* Reshaping and redisplaying is useless */
  170.                 gw->needredisplay = FALSE;        /* this round. Change is inevitable */
  171.                 // Let the change be made through the gui
  172.                 if(gw->window->Flags & WFLG_DRAGBAR)
  173.                     ChangeWindowBox(gw->window,gw->winx,gw->winy,gw->winwidth,gw->winheight);
  174.             }
  175.  
  176.             if(gw->needreshapegui)
  177.             {
  178.                 gw->needreshapegui = FALSE;
  179.                 if(gw->reshapefunc)
  180.                     gw->reshapefunc(WIN_WIDTH(gw->window), WIN_HEIGHT(gw->window));
  181.             }
  182.  
  183.             if(gw->needredisplay)
  184.             {
  185.                 gw->needredisplay = FALSE;
  186.                 if(gw->displayfunc)
  187.                     gw->displayfunc();
  188.             }
  189.             if(gw->needvisibility)
  190.             {
  191.                 gw->needvisibility = FALSE;
  192.                 if(gw->visibilityfunc)
  193.                     gw->visibilityfunc(gw->visible ? GLUT_VISIBLE : GLUT_NOT_VISIBLE);
  194.             }
  195.             if((gw->leftmenu && gw->leftmenu->needupdate) || gw->needleftmenu)
  196.             {
  197.                 gw->needleftmenu = FALSE;
  198.                 RedoMenu(GLUT_LEFT_BUTTON, gw->leftmenu);
  199.             }
  200.             if((gw->middlemenu && gw->middlemenu->needupdate) || gw->needmiddlemenu)
  201.             {
  202.                 gw->needmiddlemenu = FALSE;
  203.                 RedoMenu(GLUT_MIDDLE_BUTTON, gw->middlemenu);
  204.             }
  205.             if((gw->rightmenu && gw->rightmenu->needupdate) || gw->needrightmenu)
  206.             {
  207.                 gw->needrightmenu = FALSE;
  208.                 RedoMenu(GLUT_RIGHT_BUTTON, gw->rightmenu);
  209.             }
  210.         }
  211.  
  212.         for(gm = glutstuff.menus; gm; gm = gm->next)
  213.         {
  214.             stuffMakeCurrentMenu(gm);
  215.             gm->needupdate = FALSE;
  216.         }
  217.  
  218.         // Wait for something to happen
  219.         //
  220.         if(glutstuff.idlefunc == NULL)
  221.             Wait(1L<<glutstuff.msgport->mp_SigBit);
  222.  
  223.         // Handle all messages (if any)
  224.         //
  225.         idleing = TRUE;
  226.         while(msg = (struct IntuiMessage *)GetMsg(glutstuff.msgport))
  227.         {
  228.             cmsg = *msg;
  229.             cmsg.MouseX -= cmsg.IDCMPWindow->BorderLeft;
  230.             cmsg.MouseY -= cmsg.IDCMPWindow->BorderTop;
  231.  
  232.             ReplyMsg((struct Message *)msg);
  233.  
  234.             stuffMakeCurrent((struct GlutWindow *)(cmsg.IDCMPWindow->UserData));
  235.  
  236.             glutstuff.curwin->qualifiers = cmsg.Qualifier;
  237.  
  238.             switch(cmsg.Class)
  239.             {
  240.                 case IDCMP_CHANGEWINDOW:
  241.                     idleing = FALSE;
  242.                     glutstuff.curwin->winx = glutstuff.curwin->window->LeftEdge;
  243.                     glutstuff.curwin->winy = glutstuff.curwin->window->TopEdge;
  244.                     glutstuff.curwin->winwidth = glutstuff.curwin->window->Width;
  245.                     glutstuff.curwin->winheight = glutstuff.curwin->window->Height;
  246.                     if((glutstuff.curwin->window->Width != glutstuff.curwin->wincurwidth) ||
  247.                        (glutstuff.curwin->window->Height != glutstuff.curwin->wincurheight))
  248.                     {
  249.                         glutstuff.curwin->wincurwidth = glutstuff.curwin->window->Width;
  250.                         glutstuff.curwin->wincurheight = glutstuff.curwin->window->Height;
  251.                         glutstuff.curwin->needreshapegui = TRUE;
  252.                         glutstuff.curwin->needredisplay = TRUE;
  253.                     }
  254.                     if((glutstuff.curwin->window->LeftEdge != glutstuff.curwin->wincurx) ||
  255.                        (glutstuff.curwin->window->TopEdge != glutstuff.curwin->wincury))
  256.                     {
  257.                         glutstuff.curwin->wincurx = glutstuff.curwin->window->LeftEdge;
  258.                         glutstuff.curwin->wincury = glutstuff.curwin->window->TopEdge;
  259.                         glutstuff.curwin->needpositiongui = TRUE;
  260.                     }
  261.                     break;
  262.                 case IDCMP_VANILLAKEY:
  263.                     if(glutstuff.curwin->keyboardfunc)
  264.                     {
  265.                         idleing = FALSE;
  266.                         glutstuff.curwin->keyboardfunc(cmsg.Code,cmsg.MouseX,cmsg.MouseY);
  267.                     }
  268.                     break;
  269.                 case IDCMP_RAWKEY:
  270.                     if(glutstuff.curwin->specialfunc)
  271.                     {
  272.                         idleing = FALSE;
  273.                         glutstuff.curwin->specialfunc(ConvRaw(cmsg.Code,cmsg.Qualifier),cmsg.MouseX,cmsg.MouseY);
  274.                     }
  275.                     break;
  276.                 case IDCMP_MENUPICK:
  277.                     menuNumber = cmsg.Code;
  278.                     while(menuNumber != MENUNULL)
  279.                     {
  280.                         item = ItemAddress(glutstuff.curwin->menu, menuNumber);
  281.                         gme = (struct GlutMenuEntry *)GTMENUITEM_USERDATA(item);
  282.                         stuffMakeCurrentMenu(gme->menu);
  283.                         if(glutstuff.curmenu->menufunc)
  284.                         {
  285.                             idleing = FALSE;
  286.                             glutstuff.curmenu->menufunc(gme->value);
  287.                         }
  288.                         menuNumber = item->NextSelect;
  289.                     }
  290.                     break;
  291.                 case IDCMP_CLOSEWINDOW:
  292.                     if(wanttoquit)
  293.                     {
  294.                         // User wants to quit, but ESC doesn't do anything
  295.                         // Panic, and return in the hope that it will drop
  296.                         // through to an end-of-program call.
  297.                         // (Note that we don't use exit(), as we may want
  298.                         // to put all this in a run-time library).
  299.                         return;
  300.                     }
  301.                     if(glutstuff.curwin->keyboardfunc)
  302.                     {
  303.                         idleing = FALSE;
  304.                         glutstuff.curwin->keyboardfunc(27,0,0);
  305.                     }
  306.                     wanttoquit = TRUE;
  307.                     break;
  308.                 case IDCMP_MOUSEBUTTONS:
  309.                     if(glutstuff.curwin->mousefunc)
  310.                     {
  311.                         idleing = FALSE;
  312.                         glutstuff.curwin->mousefunc(ConvToButton(cmsg.Code,cmsg.Qualifier),ConvToButtonState(cmsg.Code),cmsg.MouseX,cmsg.MouseY);
  313.                     }
  314.                     break;
  315.                 case IDCMP_INTUITICKS:
  316.                     if((glutstuff.curwin->mousex != cmsg.MouseX) ||
  317.                        (glutstuff.curwin->mousey != cmsg.MouseY))
  318.                     {
  319.                         glutstuff.curwin->mousex = cmsg.MouseX;
  320.                         glutstuff.curwin->mousey = cmsg.MouseY;
  321.                         if((cmsg.Qual